/*
 * Copyright 2004-2005 公司名
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）* 复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.companyname.cn/licenses
 */

package com.companyname.common.base.web.view;

import java.util.List;
import java.util.ArrayList;

/**
 * 记录明细标题
 *
 */

public class Title
{
        private String text;

        private String name;

        private String onClickJSFunc;

        private boolean printed= true;

        // 父标题
        private Title superTitle;

        // 子标题
        private List subTitles = new ArrayList();

        // 深度 for 缓存
        private int treeDepth = -1;

        // 叶子总数 for 缓存
        private int maxSumOfSubTitlesLeafs = -1;

        // 到根节点的高度
        private int treeHighth = -1;
        
        // 文本长度
        private int length;

        public Title()
        {

        }

        public Title(String name,boolean isPrinted)
        {
                this.name = name;
                this.text = name;
                this.printed = isPrinted;
        }

        public Title(String beforeText,String name,String afterText,boolean isPrinted)
        {
                this.name = name;
                this.text = beforeText + name + afterText;
                this.printed = isPrinted;
        }

        public Title(String beforeText,String name,String afterText)
        {
                this.name = name;
                this.text = beforeText + name + afterText;
        }

        public Title(String name)
        {
                this.name = name;
                this.text = name;
        }

        public Title(String name,
                     String onClickJSFunc)
        {
                this.name = name;
                this.text = name;
                this.onClickJSFunc = onClickJSFunc;
        }

        public String getName()
        {
                return this.name;
        }

        public Title setName(String name)
        {
                this.name = name;
                return this;
        }

        public String getText()
        {
                return this.text;
        }

        public Title setText(String text)
        {
                this.text = text;
                return this;
        }

        public String getOnClickJSFunc()
        {
                return this.onClickJSFunc;
        }

        public boolean getPrinted()
        {
                return this.printed;
        }

        public Title setPrinted(boolean printed)
        {
                this.printed = printed;
                return this;
        }

        // 获取父标题
        public Title getSuperTitle()
        {
                return this.superTitle;
        }

        // 设置父标题
        public Title setSuperTitle(Title title)
        {
                this.superTitle = title;
                return this;
        }

        public List getSubTitles()
        {
                return this.subTitles;
        }

        public Title setSubTitles(List subTitles)
        {
                this.subTitles = subTitles;
                return this;
        }

        // 添加子标题
        public Title addSubTitle(Title title)
        {
                this.subTitles.add(title);
                title.setSuperTitle(this);
                return this;
        }

        public int getTreeHighth()
        {
                if(this.treeHighth >= 0)
                {
                        return this.treeHighth;
                }
                if(this.superTitle != null)
                {
                        int superTreeHighth = this.superTitle.getTreeHighth();
                        if( superTreeHighth == 0)
                        {
                                this.treeHighth = 1;
                                return 1;
                        }
                        else
                        {
                                superTreeHighth ++;
                                this.treeHighth = superTreeHighth;
                                return superTreeHighth;
                        }
                }
                else
                {
                        this.treeHighth = 0;
                        return 0;
                }
        }

        // 取得当前节点到叶子的深度
        public int getTreeDepth()
        {
                if(this.treeDepth >= 0)
                {
                        return this.treeDepth;
                }
                int max = 0;
                if(this.subTitles.size() != 0)
                {
                        for(int i = 0; i < this.subTitles.size(); i ++)
                        {
                                Title subTitle = (Title)this.subTitles.get(i);
                                int subDepth = subTitle.getTreeDepth();
                                if(subDepth == 0)
                                {
                                        subDepth ++;
                                }
                                if(subDepth > max)
                                {
                                        max = subDepth;
                                }
                        }
                        this.treeDepth = max + 1;
                        return max + 1;
                }
                else
                {
                        this.treeDepth = 0;
                        return 0;
                }
        }

        // 取得叶子节点数
        public int getSumOfLeafs()
        {
                int sum = 0;
                if(this.subTitles.size() != 0)
                {
                        for(int i = 0; i < this.subTitles.size(); i ++)
                        {
                                Title subTitle = (Title)this.subTitles.get(i);
                                int tempSum = subTitle.getSumOfLeafs();
                                if(tempSum == 0)
                                {
                                        sum = sum + 1;
                                }
                                else
                                {
                                        sum = sum + tempSum;
//                                        if(tempSum > sum)
//                                        {
//                                                sum = tempSum;
//                                        }
                                }
                        }
                        return sum;
                }
                else
                {
                        return 0;
                }
        }

        // 取得子节点的最大叶子数
        public int getMaxSumOfSubTitlesLeafs()
        {
                if(this.maxSumOfSubTitlesLeafs >= 0)
                {
                        return this.maxSumOfSubTitlesLeafs;
                }
                int maxSum = 0;
                for(int i = 0; i < this.subTitles.size(); i ++)
                {
                        Title title = (Title)this.subTitles.get(i);
                        int sum = title.getSumOfLeafs();
                        if(sum > maxSum)
                        {
                                maxSum = sum;
                        }
                }
                this.maxSumOfSubTitlesLeafs = maxSum;
                return maxSum;
        }

        /**
         * @return 返回 length。
         */
        public int getLength()
        {
                if(this.length == 0)
                {
                        if(this.name.length() < this.text.length())
                        {
                                return (this.name.length());
                        }
                        else
                        {
                                return (this.text.length());
                        }
                }
                else
                {
                        return this.length;
                }
        }

        /**
         * @param length 要设置的 length。
         */
        public Title setLength(int length)
        {
                this.length = length;
                return this;
        }
}
